﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevTrends.MvcDonutCaching;
using KMF.Common;
using KMF.DAL;
using KMF.WebUI.CustomHelpers;
using KMF.WebUI.Models;
using System.Configuration;
namespace KMF.WebUI.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/
        public ActionResult RidirectSearch(string keyword = "")
        {
            if (string.IsNullOrEmpty(keyword))
            {
                return RedirectToAction("Index", "Home");
            }
            return RedirectToAction("Index", "Search", new { keyword = keyword.ReplaceSpecialAsEmpty() });
        }
        public JsonResult SearchAutoComplete(string query = "")
        {
            var model = ProductDb.GetProductNameListByKeyword(query) ?? new List<ProductNameListDto>();
            var strArr = model.Select(item => item.Name).ToList();
            var data = new
            {
                query,
                suggestions = strArr
            };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        //public ActionResult Index(string keyword = "", int page = 1)
        //{
        //    ViewBag.Keyword = keyword;
        //    ViewBag.Page = page;
        //    return View();
        //}

        [DonutOutputCache(CacheProfile = "OneDay")]
        public ActionResult Index(string keyword = "")
        {
            var totalRecord = 0;
            var model = ProductDb.GetAllProductByKeyword(ref totalRecord, FunctionHelpers.RemoveSpecialCharsV2(keyword)) ?? new List<ProductRateByCateTopListPagingDto>();
            ViewBag.Keyword = keyword;
            ViewBag.TotalRecod = totalRecord;
            return View(model);
        }
    }
}
